package com.hmyd.hmyd_android.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.Constants;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.User;
import com.hmyd.hmyd_android.dto.Version;
import com.hmyd.hmyd_android.ui.CalculatorActivity;
import com.hmyd.hmyd_android.ui.ExclusiveServiceActivity;
import com.hmyd.hmyd_android.ui.FeedBackActivity;
import com.hmyd.hmyd_android.ui.InsuranceActivity;
import com.hmyd.hmyd_android.utils.DoShare;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.NormalDialog;
import com.hmyd.hmyd_android.utils.NormalDialogListener;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.sso.UMSsoHandler;

/**
 * 更多
 * 
 */
public class MoreFragment extends BasicFragment implements OnClickListener{
	View root;
	
	private final int GETLASTVERSION = 1;// 检查版本
	private final int BIND_QQ = 2;// 绑定客服QQ
	private final int GETMYINFO = 3;// 获取用户信息

	private TextView tv_version;

	private static int[] ids = { R.id.tv_more_service, R.id.tv_more_problem, R.id.tv_more_security,
			R.id.tv_more_calculator, R.id.tv_more_share, R.id.tv_more_feedback,
			R.id.tv_more_option, R.id.tv_more_aboutus, R.id.layout_version, R.id.btn_more_contact };
	
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_more_service:
			onRequest(GETMYINFO, UrlConstant.GETMYINFO, HttpMethod.GET, null);
			break;
		case R.id.tv_more_problem:
			if (AppBasic.app != null && !TextUtils.isEmpty(AppBasic.app.helpCenterLinkUrl)) {
				Utils.jumpURL(getActivity(), AppBasic.app.helpCenterLinkUrl, "常见问题", "", false);
			}
			break;
		case R.id.tv_more_security:
			startActivity(new Intent(getActivity(), InsuranceActivity.class));
			break;
		case R.id.tv_more_calculator:
			startActivity(new Intent(getActivity(), CalculatorActivity.class));
			break;
		case R.id.tv_more_share:
			if (null != AppBasic.app && !TextUtils.isEmpty(AppBasic.app.appDownload)) {
				UMSocialService mController = UMServiceFactory.getUMSocialService("myshare");
				DoShare.initSocialSDK(getActivity(), mController, "国泰·惠民益贷",
						AppBasic.app.appDownload, AppBasic.app.couponUseInstructions, true);
			}
			break;
		case R.id.tv_more_feedback:
			startActivity(new Intent(getActivity(), FeedBackActivity.class));
			break;
		case R.id.tv_more_option:
			break;
		case R.id.tv_more_aboutus:
			if (AppBasic.app != null && !TextUtils.isEmpty(AppBasic.app.helpCenterLinkUrl)) {
				Utils.jumpURL(getActivity(), AppBasic.app.aboutLinkUrl, "关于国泰·惠民益贷", "", false);
			}
			break;
		case R.id.layout_version:
			onRequest(GETLASTVERSION, UrlConstant.GETLASTVERSION, HttpMethod.GET, null);
			break;
		case R.id.btn_more_contact:
			//4009691688
			Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:400-969-1688"));
			startActivity(intent);
			break;

		default:
			break;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (root == null) {
			root = inflater.inflate(R.layout.layout_more, container, false); // 加载fragment布局
		}
		//缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
		ViewGroup parent = (ViewGroup) root.getParent();
		if (parent != null) {
			parent.removeView(root);
		} 
		return root;
	}
	
	private void setParams(){
		tv_version = (TextView) root.findViewById(R.id.more_tv_version);
		
		for (int i = 0; i < ids.length; i++) {
			root.findViewById(ids[i]).setOnClickListener(this);
		}
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		setParams();
		
		super.onActivityCreated(savedInstanceState);
		tv_version.setText("V" + Utils.getVersionName(getActivity()));
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		switch (postId) {
		case GETLASTVERSION:
			java.lang.reflect.Type versionType = new TypeToken<Version>() {
			}.getType();
			final Version version = gson.fromJson(result, versionType);
			if (version != null && version.hasNew) {
				NormalDialog cd = new NormalDialog(getActivity(), R.style.calculatorDialog,
						new NormalDialogListener() {
							@Override
							public void ok() {
								Utils.jumpExplorer(getActivity(), version.newVersionUrl);
							}

							@Override
							public void cancle() {

							}

						}, version.infoTitle, version.info, "", "");
				cd.show();
			} else if (version != null && !version.hasNew) {
				MyToast.showToast(getActivity(), version.info);
			}
			break;
		case GETMYINFO:
			java.lang.reflect.Type type2 = new TypeToken<User>() {
			}.getType();
			User user = gson.fromJson(result, type2);

			if (user.isBind) {
				Utils.openSession(getActivity(), Constants.QQ_PACKAGENAME, user.bindQQ);
			} else {
				startActivityForResult(new Intent(getActivity(), ExclusiveServiceActivity.class),
						99);
			}

			break;
		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 99 && resultCode == getActivity().RESULT_OK) {
			String qq = data.getStringExtra("qq");
			Utils.openSession(getActivity(), Constants.QQ_PACKAGENAME, qq);
		}

		UMSsoHandler ssoHandler = SocializeConfig.getSocializeConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}

	}

}
